#!/bin/sh
#
# maxscale:   The MariaDB Corporation MaxScale database proxy
#
# description:  MaxScale provides database specific proxy functionality
#
# processname: maxscale
#
### BEGIN INIT INFO
# Provides: maxscale
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: The maxscale database proxy
# Description: MaxScale is a database proxy server that can be used to front end
# database clusters offering different routing, filtering and protocol choices
### END INIT INFO

#############################################
# MaxScale PIDFILE and LIB
#############################################

export MAXSCALE_PIDFILE=@MAXSCALE_VARDIR@/run/maxscale/maxscale.pid
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:@CMAKE_INSTALL_PREFIX@/@MAXSCALE_LIBDIR@/maxscale

###############################
# LSB Exit codes (non-Status)
###############################
_RETVAL_GENERIC=1
_RETVAL_NOT_INSTALLED=5
_RETVAL_NOT_RUNNING=7

###############################
# LSB Status action Exit codes
###############################
_RETVAL_STATUS_OK=0
_RETVAL_STATUS_NOT_RUNNING=3

# Create PID directory if not exists
/usr/bin/install -d -o maxscale -g maxscale @MAXSCALE_VARDIR@/run/maxscale

# Sanity checks.
[ -x @CMAKE_INSTALL_PREFIX@/@MAXSCALE_BINDIR@/maxscale ] || exit $_RETVAL_NOT_INSTALLED

# Source function library.
. /etc/rc.d/init.d/functions

# Source additional command line arguments.
[ -f /etc/sysconfig/maxscale ] &&  . /etc/sysconfig/maxscale

# we can rearrange this easily
processname=maxscale
servicename=maxscale

RETVAL=0

start() {
    echo -n $"Starting MaxScale: "
    my_check=`status -p $MAXSCALE_PIDFILE @CMAKE_INSTALL_PREFIX@/@MAXSCALE_BINDIR@/maxscale`
    CHECK_RET=$?
    [ $CHECK_RET -eq 0 ] && echo -n " found $my_check" && success && CHECK_RET=0

    if [ ! -d @MAXSCALE_VARDIR@/log/maxscale ]
    then
        mkdir -p @MAXSCALE_VARDIR@/log/maxscale
    fi

    if [ ! -d @MAXSCALE_VARDIR@/cache/maxscale ]
    then
        mkdir -p @MAXSCALE_VARDIR@/cache/maxscale
    fi

    if [ ! -d @MAXSCALE_VARDIR@/lib/maxscale ]
    then
        mkdir -p @MAXSCALE_VARDIR@/lib/maxscale
    fi

    if [ ! -d @MAXSCALE_VARDIR@/run/maxscale ]
    then
        mkdir -p @MAXSCALE_VARDIR@/run/maxscale
    fi

    chown -R maxscale:maxscale @MAXSCALE_VARDIR@/log/maxscale
    chown -R maxscale:maxscale @MAXSCALE_VARDIR@/lib/maxscale
    chown -R maxscale:maxscale @MAXSCALE_VARDIR@/cache/maxscale
    chown -R maxscale:maxscale @MAXSCALE_VARDIR@/run/maxscale
    chmod 0755 @MAXSCALE_VARDIR@/log/maxscale
    chmod 0755 @MAXSCALE_VARDIR@/lib/maxscale
    chmod 0755 @MAXSCALE_VARDIR@/cache/maxscale
    chmod 0755 @MAXSCALE_VARDIR@/run/maxscale

    daemon --pidfile $MAXSCALE_PIDFILE @CMAKE_INSTALL_PREFIX@/@MAXSCALE_BINDIR@/maxscale --user=maxscale $MAXSCALE_OPTIONS >& /dev/null

    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$servicename

    if [ $CHECK_RET -ne 0 ]; then
	sleep 2
        my_check=`status -p $MAXSCALE_PIDFILE @CMAKE_INSTALL_PREFIX@/@MAXSCALE_BINDIR@/maxscale`
        CHECK_RET=$?
        [ $CHECK_RET -eq 0 ] && echo -n $my_check && success || failure
    fi

    # Return right code
    if [ $RETVAL -ne 0 ]; then
       failure
       RETVAL=$_RETVAL_NOT_RUNNING
    fi

    echo

    return $RETVAL
}

stop() {
    echo -n $"Stopping MaxScale: "
    killproc -p $MAXSCALE_PIDFILE -TERM

    RETVAL=$?

    echo

    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$servicename

    # Return rigth code
    if [ $RETVAL -ne 0 ]; then
       RETVAL=$_RETVAL_NOT_RUNNING
    fi

    return $RETVAL
}

reload() {
    echo -n $"Reloading MaxScale: "

    killproc -p $MAXSCALE_PIDFILE @CMAKE_INSTALL_PREFIX@/@MAXSCALE_BINDIR@/maxscale -HUP
    RETVAL=$?
    echo
}

# See how we were called.
case "$1" in
    start)
	start
        ;;
    stop)
        stop
        ;;
    status)
	# return 0 on success
	# return 3 on any error

    	echo -n $"Checking MaxScale status: "
        status -p $MAXSCALE_PIDFILE 'MaxScale'
        RETVAL=$?

	if [ $RETVAL -ne 0 ]; then
		echo -ne "\033[1A"
		[ $RETVAL -eq 1 ] && warning || failure
		echo -ne "\033[1B"

		RETVAL=$_RETVAL_STATUS_NOT_RUNNING
	else
		echo -ne "\033[1A"
		success
		echo -ne "\033[1B"
		RETVAL=$_RETVAL_STATUS_OK
	fi

	exit $RETVAL
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$servicename ]; then
            stop
            start
        fi
        ;;
    reload)
	reload
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        ;;
esac
exit $RETVAL
